package com.beiding.businessplatform.data;

import java.util.List;

public abstract class Vm {

    private Class beType;

    private Be be;

    private VmCache vmCache;

    private HolderMap holderMap;

    private int mode;

    public Vm(Class beType, BeHelp beHelp) {
        this.beType = beType;
        holderMap = beHelp.createBeHolder(() -> be());
        vmCache = new VmCache(this);
    }

    void mode(int mode) {
        this.mode = mode;
    }

    int getMode() {
        return mode;
    }

    //默认提供方法

    public String getId() {
        return (String) vmCache.get("id");
    }

    public void setId(String id) {
        set("id", id);
    }


    VmCache vmCache() {
        return vmCache;
    }

    Be be() {
        if (be == null) {
            be = BeConfig.getBeMaker().make(beType);
        }
        return be;
    }

    void be(Be be) {
        if (this.be == be) {
            return;
        }
        this.be = be;
        vmCache.handle(be);
    }

    void update() {
        vmCache.update();
    }

    HolderMap holderMap() {
        return holderMap;
    }

    //基础值获取
    protected Object getValue(String field) {
        return vmCache.get(field);
    }

    protected List getVmList(String field, Class<? extends Vm> type) {
        return vmCache.getVmList(field, type);
    }

    protected Vm getVm(String field, Class<? extends Vm> type) {
        return vmCache.getVm(field, type);
    }

    protected void set(String field, Object value) {
        vmCache.set(field, value);
    }

    protected void set(String field, Vm value) {
        vmCache.set(field, value);
    }

    public void set(String field, List<? extends Vm> value) {
        vmCache.set(field, value);
    }


}
